<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList"
          size="s米糕"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch"
          size="s米糕"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="s米糕"
          label-width="140px"
        >
          <el-form-item label="电邀姓名：">
            <el-select
              v-model="listQuery.invitationId"
              clearable
              filterable
              placeholder="请选择"
            >
              <el-option-group
                v-for="item in invitationDepartment"
                :key="item.id"
                :label="item.name"
              >
                <el-option
                  v-for="user in item.users"
                  :key="user.id"
                  :label="user.name"
                  :value="user.id"
                >
                </el-option>
              </el-option-group>
            </el-select>
          </el-form-item>

          <el-form-item label="城市：">
            <el-select
              v-model="listQuery.cityId"
              clearable
              filterable
              placeholder="请选择城市"
            >
              <el-option
                v-for="item in citys.options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="派单类型：">
            <el-select
              v-model="listQuery.assignType"
              clearable
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in assignType"
                :key="item.id"
                :label="item.value"
                :value="item.id - 1"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="派单日期：">
            <el-date-picker
              v-model="searchAskForLeave"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
              clearable
            >
              >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table
        ref="adminTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="派单日期" align="center">
          <template slot-scope="scope">{{
            formatDate(new Date(scope.row.createTime), "yyyy/MM/dd")
          }}</template>
        </el-table-column>
        <el-table-column label="派单类型" align="center">
          <template slot-scope="scope">{{
            assignType[scope.row.assignType - 1].value
          }}</template>
        </el-table-column>

        <el-table-column label="电邀姓名" align="center">
          <template slot-scope="scope">
            {{ scope.row.staffName }}
          </template>
        </el-table-column>

        <el-table-column label="派单数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.assignCount }}
          </template>
        </el-table-column>

        <el-table-column label="实际派单数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.actualNum }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleShowList(scope.$index, scope.row)"
              >查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div
      class="pagination-container"
      style="
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        padding-bottom: 20px;
      "
    >
      <el-pagination
        background
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :current-page.sync="listQuery.pageNum"
        :page-size="pageSize"
        :total="total"
      >
      </el-pagination>
    </div>

    <companyPage
      :id="selectedId"
      :assignDate="assignDate"
      :dialogVisible="dialogVisible"
      @close="closeWindow"
    ></companyPage>
  </div>
</template>
<script>
import {
  getObtainOrganizationalStructure,
  getCitys,
  getAssignHistorySearch,
  getAssignHistory,
} from "../../api/assign";
import {
  DEPARTMENT_SEARCH_BY_HISTORY_SEARCH,
  ASSIGN_ORDER_TYPE,
} from "../../contanst/data";
import { getStartAndEndTime } from "../../utils/time";
import { formatDate } from "@/utils/date";
import {parsePage} from "../../utils/page"
export default {
  name: "assignConfig",
  data() {
    return {
      listQuery: Object.assign({}, DEPARTMENT_SEARCH_BY_HISTORY_SEARCH),
      list: [],
      invitationDepartment: [],
      assignType: ASSIGN_ORDER_TYPE,
      currentPage: 1,
      lastId: 0,
      selectedId: -1,
      assignDate: "",
      companys: [],
      searchAskForLeave: undefined,
      listLoading: false,
      dialogVisible: false,
      citys: {},
      pageSize: 50,
      total: 0,
    };
  },

  
  created() {
    getObtainOrganizationalStructure({}, this);

    getCitys(this);
  },

  mounted() {
    console.log(65+65+69784978978978);
    getAssignHistory(this.listQuery, this);
  },

  components: {
    companyPage: () => import("./component/read.vue"),
  },

  methods: {
    formatDate,
    handleShowList(index, item) {
      this.selectedId = item.id;

      this.assignDate = formatDate(this.list[0].createTime, "yyyy/MM/dd");

      this.dialogVisible = true;

      console.log("dialogVisible", this.dialogVisible);
    },

    closeWindow(visiable) {
      console.log("visiable", visiable);
      this.dialogVisible = visiable;
    },

  
    //排序 倒序
    sortList(f, s) {
      this.list.length &&
        this.list.sort(function (a, b) {
          return f.id - s.id;
        });
    },

    // 弹窗关闭回调
    closed() {
      this.$refs.adminForm.clearValidate();
    },

    handleResetSearch() {
      this.status = "";
      this.cityValue = null;
      this.listQuery = Object.assign({}, DEPARTMENT_SEARCH_BY_HISTORY_SEARCH);
      this.searchAskForLeave = undefined;
      this.handleSearchList();
    },
    async handleSearchList() {
      this.listLoading = true;

      let time = getStartAndEndTime(this.searchAskForLeave);

      if (0 != time.length) {
        this.listQuery.startTime = time[0];

        this.listQuery.endTime = time[1];
      }

      console.log("searchAskForLeave", this.searchAskForLeave);

      getAssignHistorySearch(this.listQuery, this);

      this.listLoading = false;
    },

    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      
      let param = {...this.listQuery};

      param.lastId = this.lastId;

      getAssignHistorySearch(param, this);

    },
      
    
    
  },

  watch: {
      "listQuery.pageNum":function(newVal, oldValue) {

        parsePage.bind(this)(newVal, oldValue, "list");

      }
      
    }
};
</script>
  <style scoped>
.my-dialog :deep(.el-dialog) {
  min-width: 650px;
  max-width: 100%;
}
.table-container {
  display: flex;
  flex-direction: column;
  flex: auto;
  background-color: red;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  margin-left: 5px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
    
    
    